﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Job.Logic.Common;
using Job.Entity;
using System.Data;
using Job.Web.Framework;
using Job.Data.Interfaces;

namespace Job.Logic.Company
{
    public class SearcherLogic
    {
        IJobCompanySearcherDao dao = DataAccess.companySearcherDao;
        public int GetCountByAccount(int companyId, string loginCode)
        {
            return dao.GetCountByAccount(companyId, loginCode);
        }

        //企业管理自己的个人搜索器时显示的列表
        public DataTable GetPageSearcherByAccount(int companyId, string loginCode, int pageSize, int pageIdx)
        {
            loginCode = (null == loginCode) ? "" : loginCode.Trim().Replace("'", "''");
            string sql = "Select searcher_id,searcher_name,is_senior,person_class,create_time "
                + "From job_company_searcher Where creator_company=" + companyId.ToString()
                + " and creator_logincode='" + loginCode + "'";

            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIdx, 0,
                "create_time", SortDirection.Descending, null);

            return ds.Tables[0];
        }

        public JobCompanySearcher GetSearcher(int id)
        {
            return dao.Find(id);
        }

        public void InsertSearcher(JobCompanySearcher searcher)
        {
            dao.Insert(searcher);
        }
        public void UpdateSearcher(JobCompanySearcher searcher)
        {
            dao.UpdateByAccount(searcher);
        }
        public void DeleteSearcher(JobCompanySearcher searcher)
        {
            dao.DeleteByAccount(searcher);
        }

        //按照搜索器中定义的搜索条件查询符合条件的求职者数量
        public int CountPersonBySearcher(JobCompanySearcher searcher,string companyName)
        {
            string sql = "Select * From JOB_PERSON_INFO Where " + GetWhere(searcher);
            if ( companyName != null && companyName.Length > 0 ) 
                sql = sql + " And " + companyName + " not like '%' + skip_company + '%' ";
            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, 10, 1, 1,
                "last_refresh", SortDirection.Descending, null);
            return (Int32)ds.Tables[0].Rows[0][0];
        }

        //按照搜索器中定义的搜索条件查询符合条件的求职者中的某一页列表
        public DataTable GetPagePersonBySearcher(JobCompanySearcher searcher,string companyName,
            int pageSize, int pageIdx)
        {
            string sql = "Select person_info_id,user_name,user_sex,grad_schoole,high_edu,work_len,"
                + "job_status,reside_addr,last_refresh From JOB_PERSON_INFO Where " + GetWhere(searcher);
            if (companyName != null && companyName.Length > 0)
                sql = sql + " And " + companyName + " not like '%' + skip_company + '%' ";

            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIdx, 0,
                "last_refresh", SortDirection.Descending, null);

            return ds.Tables[0];
        }

        private string GetWhere(JobCompanySearcher searcher)
        {
            StringBuilder buf = new StringBuilder();
            buf.Append(" 1=1 ");
            if ( searcher.PersonLoginName != null && searcher.PersonLoginName.Length > 0 )
            {
                buf.Append (" And login_name like '%");
                buf.Append(searcher.PersonLoginName.Replace("'","''"));
                buf.Append("%' ");
            }

            if ( searcher.LastLoginDiff > 0 ) 
            {
                buf.Append(" And last_login_date >= ");
                buf.Append("cast(cast(getdate() - ");
                buf.Append(searcher.LastLoginDiff.ToString());
                buf.Append(" as varchar(10)) as datetime) ");
            }

            if ( searcher.LastCreateDiff > 0 ) 
            {
                buf.Append(" And create_date >= ");
                buf.Append("cast(cast(getdate() - ");
                buf.Append(searcher.LastCreateDiff.ToString());
                buf.Append(" as varchar(10)) as datetime) ");
            }

            if (searcher.LastRefreshDiff > 0 )
            {
                buf.Append(" And last_refresh >= ");
                buf.Append("cast(cast(getdate() - ");
                buf.Append(searcher.LastRefreshDiff.ToString());
                buf.Append(" as varchar(10)) as datetime) ");
            }

            if (searcher.PersonName != null && searcher.PersonName.Length > 0)
            {
                buf.Append(" And user_name like '%");
                buf.Append(searcher.PersonName.Replace("'","''"));
                buf.Append("%' ");
            }

            if (searcher.PersonSex != null && searcher.PersonSex.Length > 0 && !searcher.PersonSex.Equals("0"))
            {
                buf.Append(" And user_sex='" + searcher.PersonSex + "' ");
            }
            
            if ( searcher.AgeMin > 0 ) 
            {
                buf.Append(" And ( year(birth_date) <= (year(getdate()) - ");
                buf.Append(searcher.AgeMin.ToString());
                buf.Append(")) ");
            }

            if ( searcher.AgeMax > 0 ) 
            {
                buf.Append(" And ( year(birth_date) >= (year(getdate()) - ");
                buf.Append(searcher.AgeMax.ToString());
                buf.Append(")) ");
            }

            if ( searcher.HomeAddr != null && searcher.HomeAddr.Length >= 4 )
            {
                buf.Append(" And home_addr = '");
                buf.Append(searcher.HomeAddr);
                buf.Append("' ");
            }
            else if ( searcher.HomeAddr != null && searcher.HomeAddr.Length > 1 )
            {
                buf.Append(" And home_addr like '");
                buf.Append(searcher.HomeAddr);
                buf.Append("%' ");
            }

            if ( searcher.ResideAddr != null && searcher.ResideAddr.Length >= 4 )
            {
                buf.Append(" And reside_addr = '");
                buf.Append(searcher.HomeAddr);
                buf.Append("' ");
            }
            else if ( searcher.ResideAddr != null && searcher.ResideAddr.Length > 1)
            {
                buf.Append(" And reside_addr like '");
                buf.Append(searcher.HomeAddr);
                buf.Append("%' ");
            }

            //学历必须预先约定好，按学历从低到高插入数据字典中            
            if ( searcher.EduMin > 0 )
            {
                buf.Append(" And high_edu >= " );
                buf.Append(searcher.EduMin.ToString());
            }
            if ( searcher.EduMax > 0 )
            {
                buf.Append(" And high_edu <= " );
                buf.Append(searcher.EduMax.ToString());
            }

            if ( searcher.SpecialtyClass != null && searcher.SpecialtyClass.Length >= 4 )
            {
                buf.Append(" And (specialty_class1='");
                buf.Append(searcher.SpecialtyClass);
                buf.Append("' Or specialty_class2='");
                buf.Append(searcher.SpecialtyClass);
                buf.Append("') ");
            }
            else if ( searcher.SpecialtyClass != null && searcher.SpecialtyClass.Length >= 1 )
            {
                buf.Append(" And (specialty_class1 like '");
                buf.Append(searcher.SpecialtyClass);
                buf.Append("%' Or specialty_class2 like '");
                buf.Append(searcher.SpecialtyClass);
                buf.Append("%') ");
            }

            if (searcher.TechTitle > 0 )
            {
                buf.Append(" And tech_title=" + searcher.TechTitle.ToString());
            }

            if (searcher.GradYearMin > 0 )
            {
                buf.Append(" And grad_year >= (year(getdate()) - ");
                buf.Append(searcher.GradYearMin.ToString());
                buf.Append(") ");
            }
            if (searcher.GradYearMax > 0 )
            {
                buf.Append(" And grad_year <= (year(getdate()) - ");
                buf.Append(searcher.GradYearMin.ToString());
                buf.Append(") ");    
            }

            if (searcher.WorkLenMin > 0 ) 
            {
                buf.Append(" And work_len >= " + searcher.WorkLenMin.ToString());
            }
            if ( searcher.WorkLenMax > 0 )
            {
                buf.Append(" And work_len <= " + searcher.WorkLenMax.ToString());
            }

            if (searcher.ForeignLang > 0 )
            {
                buf.Append(" And foreign_lang = " + searcher.ForeignLang.ToString());
            }
            //语言熟练程度需要预先约定好，按照熟练程度从低到高插入数据字典中
            if ( searcher.ForeignSkill > 0 ) 
            {
                buf.Append(" And foreign_skill >= " + searcher.ForeignSkill.ToString());
            }
            if ( searcher.DialectLang > 0)
            {
                buf.Append(" And dialect_lang >= " + searcher.DialectLang.ToString());
            }
            if ( searcher.ComputerSkill > 0 )
            {
                buf.Append(" And computer_skill >= " + searcher.ComputerSkill.ToString());
            }

            if ( searcher.JobStatus > 0 )
            {
                buf.Append(" And job_status = " + searcher.JobStatus.ToString());
            }

            if ( searcher.CurIndustry != null && searcher.CurIndustry.Length >= 4 )
            {
                buf.Append(" And cur_industry = '" );
                buf.Append(searcher.CurIndustry);
                buf.Append("' ");
            }
            else if ( searcher.CurIndustry != null && searcher.CurIndustry.Length > 1 )
            {
                buf.Append(" And cur_industry like '" );
                buf.Append(searcher.CurIndustry);
                buf.Append("%' ");
            }
            if ( searcher.ApplyStyle > 0 )
            {
                buf.Append(" And apply_style = " + searcher.ApplyStyle.ToString());
            }
            
            if ( searcher.ResumeState >= 0 )
            {
                buf.Append(" And resume_state = " + searcher.ResumeState.ToString());
            }
            if ( searcher.IsSenior == 1 )
            {
                buf.Append(" And is_senior = 1 ");
            }
            else if (searcher.IsSenior == 2)
            {
                buf.Append(" And is_senior = 0 ");
            }

            //最快到岗时间，暂时未使用   availability_day 	int
            

            if ( searcher.PersonClass > 0 ) 
            {
                buf.Append(" And person_class = " + searcher.PersonClass.ToString());
            }
            if (searcher.SearchKey != null && searcher.SearchKey.Length > 0)
            {
                string key = searcher.SearchKey.Replace("'", "''");
                buf.Append(" And (person_desc like '%");
                buf.Append(key);
                buf.Append("%' Or foreign_resume like '%");
                buf.Append(key);
                buf.Append("%' Or other_desc like '%");
                buf.Append(key);
                buf.Append("%') ");
            }

            return buf.ToString();
        }
    }
}
